package com.tgy.admin.util;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.tgy.admin.LoginUser;
import com.tgy.constant.RedisKey;
import com.tgy.pojo.SysAdmin;
import com.tgy.pojo.SysPermission;
import com.tgy.pojo.SysRole;
import com.tgy.util.RedisUtils;
import com.tgy.vo.MenuVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author tianguiyin
 * @version 1.0
 * @date 2022/6/21 19:59
 *
 * 登录用户帮助类
 */
@Component
public class LoginUserHelp {

    @Resource
    private RedisUtils redis;

    public LoginUser getLoginUser(){
        String loginId = StpUtil.getLoginId().toString();
        String info = redis.get(RedisKey.LOGIN_USER_INFO+":"+loginId);
        String role = redis.get(RedisKey.LOGIN_USER_ROLE+":"+loginId);
        String permission = redis.get(RedisKey.LOGIN_USER_PERMISSION+":"+loginId);
        String menu = redis.get(RedisKey.LOGIN_USER_MENU+":"+loginId);

        SysAdmin admin = JSONObject.parseObject(info, SysAdmin.class);
        List<SysRole> roles = StrUtil.isBlank(role) ? new ArrayList<>() : JSONObject.parseArray(role, SysRole.class);
        List<SysPermission> permissions = StrUtil.isBlank(permission) ? new ArrayList<>() : JSONObject.parseArray(permission, SysPermission.class);
        List<MenuVo> menuVos = StrUtil.isBlank(menu) ? new ArrayList<>() : JSONObject.parseArray(menu, MenuVo.class);

        return new LoginUser(admin,roles,permissions,menuVos);
    }

}
